package com.copa.gamestore.module.order.controller;

import com.copa.gamestore.common.enums.PayStatus;
import com.copa.gamestore.common.response.ResponseVo;
import com.copa.gamestore.module.order.application.OrderApplicationService;
import com.copa.gamestore.module.order.domain.Order;
import com.copa.gamestore.module.order.domain.OrderLog;
import com.copa.gamestore.module.order.dto.OrderDto;
import com.copa.gamestore.module.order.vo.OrderAdminListVo;
import com.copa.gamestore.module.order.vo.OrderVo;
import com.copa.gamestore.security.domain.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-09-27 18:01
 * @function
 */
@RestController
@RequestMapping("order")
@Api(tags = "订单服务")
public class OrderController {

    @Resource
    private OrderApplicationService orderApplicationService;

    @ApiOperation("下订单")
    @PostMapping
    @RolesAllowed(Role.USER)
    public ResponseVo<OrderVo> order(@RequestBody OrderDto orderDto) {
        return orderApplicationService.order(orderDto);
    }

    @ApiOperation("支付/取消")
    @GetMapping("pay")
    @RolesAllowed(Role.USER)
    public ResponseVo<String> pay(@RequestParam("orderSn") String orderSn,
                                  @RequestParam("payStatus") Integer payStatus) {
        if (payStatus == PayStatus.PAY.getStatus()) {
            return orderApplicationService.finishPay(orderSn);
        }
        if (payStatus == PayStatus.CANCEL.getStatus()) {
            return orderApplicationService.cancelPay(orderSn);
        }
        throw new RuntimeException();
    }

    @ApiOperation("获取个人订单列表（可根据订单状态）")
    @GetMapping("list")
    @RolesAllowed(Role.USER)
    public ResponseVo<List<Order>> list(@RequestParam(value = "status", required = false) Integer status) {
        return orderApplicationService.list(status);
    }

    @ApiOperation("根据订单编号获取订单详情")
    @GetMapping("{orderSn}")
    @RolesAllowed(Role.USER)
    public ResponseVo<OrderVo> get(@PathVariable("orderSn") String orderSn) {
        return orderApplicationService.get(orderSn);
    }

    @ApiOperation("收货")
    @GetMapping("receive")
    @RolesAllowed(Role.USER)
    public ResponseVo<String> receive(@RequestParam String orderSn) {
        return orderApplicationService.receive(orderSn);
    }

    @ApiOperation("申请退款")
    @GetMapping("refund")
    @RolesAllowed(Role.USER)
    public ResponseVo<String> refund(@RequestParam String orderSubSn) {
        return orderApplicationService.refund(orderSubSn);
    }

    @ApiOperation("审核退款")
    @GetMapping("checkRefund")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<String> checkRefund(@RequestParam String orderSubSn, @RequestParam Integer refundStatus) {
        return orderApplicationService.checkRefund(orderSubSn, refundStatus);
    }

    @ApiOperation("审核退款订单列表")
    @GetMapping("checkRefundList")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<List<OrderAdminListVo>> checkRefundList() {
        return orderApplicationService.checkRefundList();
    }

    @ApiOperation("获取所有订单列表（可根据订单状态）")
    @GetMapping("admin/list")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<List<OrderAdminListVo>> listAdmin(@RequestParam(value = "status", required = false) Integer status) {
        return orderApplicationService.listAdmin(status);
    }

    @ApiOperation("根据订单编号获取订单详情")
    @GetMapping("admin/{orderSn}")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<OrderVo> getAdmin(@PathVariable("orderSn") String orderSn) {
        return orderApplicationService.getAdmin(orderSn);
    }

    @ApiOperation("全部或根据订单编号获取日志(用户)")
    @GetMapping("log")
    @RolesAllowed(Role.USER)
    public ResponseVo<List<OrderLog>> getLog(@RequestParam(value = "orderSn", required = false) String orderSn) {
        return orderApplicationService.getLog(orderSn);
    }

    @ApiOperation("全部或根据订单编号获取日志(管理员)")
    @GetMapping("admin/log")
    @RolesAllowed(Role.ADMIN)
    public ResponseVo<List<OrderLog>> getLogAdmin(@RequestParam(value = "orderSn", required = false) String orderSn) {
        return orderApplicationService.getLogAdmin(orderSn);
    }
}
